package com.mingxing.controller;
import com.mingxing.entity.User;
import com.mingxing.service.IUserService;
import com.mingxing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;

@Controller
@RequestMapping("/api/login")
public class LoginController {

    @Autowired
    private IUserService service;

    /**
     * 登陆
     * @param request
     * @param response
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response){
        String zhanghao = request.getParameter("zhanghao");
        if(StringUtils.isEmpty(zhanghao)){
            return Result.reError("账号不能为空");
        }
        String mima =  request.getParameter("mima");
        if(StringUtils.isEmpty(mima)){
            return Result.reError("密码不能为空");
        }
        //进行登录认证
        User qUsers=this.service.login(zhanghao,mima);
        if(qUsers==null){
            return Result.reError("账号密码错误！！！");
        }
        request.getSession().setAttribute("user",qUsers);
        return Result.reOk("登录成功");
    }

    /**
     * 注册
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/register")
    @ResponseBody
    public Result register(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String realname = request.getParameter("realname");
        if(StringUtils.isEmpty(realname)){
            return Result.reError("昵称不能为空");
        }
        String zhanghao = request.getParameter("zhanghao");
        if(StringUtils.isEmpty(zhanghao)){
            return Result.reError("账号不能为空");
        }
        String mima =  request.getParameter("mima");
        if(StringUtils.isEmpty(mima)){
            return Result.reError("密码不能为空");
        }
        String sex =  request.getParameter("sex");
        if(StringUtils.isEmpty(sex)){
            return Result.reError("性别不能为空");
        }
        User qUsers=new User();
        qUsers.setRealname(realname);
        qUsers.setZhanghao(zhanghao);
        qUsers.setMima(mima);
        qUsers.setSex(sex);
        return this.service.insertDb(qUsers);
    }


    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpSession session) {
        User qUsers=(User) session.getAttribute("user");
        return Result.reOk("查询成功",qUsers);
    }


    /**
     * 修改当前登录的用户信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Result updateUserInfo(User qUsers,HttpSession session) {
        this.service.updatesDb(qUsers);
        //刷新用户
        User db=this.service.getByIds(qUsers.getId());
        session.setAttribute("user",db);
        return Result.reOk("修改成功");

    }


    /**
     * 路由地址
     */
    public String prefix = "/view/admin/account";

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


}
